<template>
  <a-modal
    :title="title"
    :width="1070"
    :visible="visible"
    :confirmLoading="loading"
    :maskClosable="false"
    :destroyOnClose="true"
    :centered="true"
    :footer="true"
    @cancel="cancel"
  >
    <a-spin :spinning="loading">
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd" style="margin-right: 10px">增加题目</a-button>
        <a-upload
          :accept="accept"
          :customRequest="onUpload"
          :showUploadList="false"
          name="iFile"
        >
          <a-button
            type="primary"
            icon="file-excel"
            :loading="uploadExcelLoading"
            style="margin-right: 10px">导入Excel
          </a-button>
        </a-upload>
        <a-button type="" icon="arrow-down" @click="downloadExcel" style="margin-right: 10px">下载导入模板
        </a-button>
        <a-button type="primary" icon="security-scan" @click="showAutoModel">在线自动识别</a-button>
        <SearchForm ref="SearchForm" @handleSubmit="handleSeachSubmit" />
      </div>
      <s-table
        ref="table"
        rowKey="qt_id"
        :loading="isLoading"
        :columns="columns"
        :data="loadData"
        :pageSize="15"
        :scroll="{y:500}"
      >
        <div slot="title1" slot-scope="text, item">
          <a-tooltip>
            <template slot="title">
              {{ text }}
            </template>
            {{ text.length > 20 ? text.slice(0, 20) + '...' : text }}
          </a-tooltip>
        </div>

        <div slot="qt_type" slot-scope="text, item">
          <a-tag color="orange" v-if="item.qt_type==1">判断题</a-tag>
          <a-tag color="green" v-if="item.qt_type==2">单选题</a-tag>
          <a-tag color="blue" v-if="item.qt_type==3">多选题</a-tag>
        </div>

        <div slot="status" slot-scope="text, item">
          <a-tag color="green" v-if="item.status==0">启用</a-tag>
          <a-tag color="red" v-if="item.status==1">停用</a-tag>
        </div>
        <div slot="amount_type" slot-scope="text, item">
          <template v-if="item.jyd==1">
            <a-tag color="green" v-if="item.amount_type==0">自行垫付</a-tag>
            <a-tag color="blue" v-if="item.amount_type==1">公司已付</a-tag>
          </template>
        </div>
        <div class="actions" slot="action" slot-scope="text, item">
          <a v-action:edit @click="handleEdit(item)">编辑</a>
          <a-popconfirm
            v-action:delete
            title="确认删除当前记录？"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleDelete(item)"
          >
            <a href="#">删除</a>
          </a-popconfirm>
        </div>
      </s-table>
      <SaveForm :question_id="queryParam.question_id" ref="SaveForm" @handleSubmit="handleRefresh" />
      <AutoModel :question_id="queryParam.question_id" ref="AutoModel" @success="handleRefresh" />
    </a-spin>
  </a-modal>
</template>
<script>
import * as Api from '@/api/question/topic'
import { STable } from '@/components'
import SearchForm from './modules/SearchForm'
import SaveForm from './modules/SaveForm'
import AutoModel from './modules/AutoModel'
import * as UploadApi from '@/api/upload'
import store from '@/store'

const publicConfig = store.getters.publicConfig

export default {
  name: 'TopicListModel',
  components: { SearchForm, SaveForm, AutoModel, STable },
  data () {
    return {
      title: '题目列表',
      queryParam: {},
      visible: false,
      loading: false,
      isLoading: false,
      vehicle_id: '',
      columns: [
        {
          title: '题目ID',
          dataIndex: 'qt_id',
          align: 'center',
          width: 60
        },
        {
          title: '题目标题',
          dataIndex: 'title',
          width: 300,
          scopedSlots: { customRender: 'title1' }
        },
        {
          title: '题目类型',
          dataIndex: 'qt_type',
          align: 'center',
          scopedSlots: { customRender: 'qt_type' }
        },
        {
          title: '正确答案',
          dataIndex: 'answer',
          align: 'center'
        },
        {
          title: '分数',
          dataIndex: 'score',
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'status',
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '创建时间',
          dataIndex: 'create_time',
          align: 'center',
          width: 200
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      currVrId: '',
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      },
      accept: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel',
      uploadExcelLoading: false,
      uploadUrl: UploadApi.excel
    }
  },
  methods: {
    onUpload (info) {
      this.uploadExcelLoading = true
      // 构建上传参数
      const formData = new FormData()
      formData.append('iFile', info.file)
      // 开始上传
      this.uploadUrl(formData)
        .then(({ data }) => {
          const fileId = data.fileInfo.file_id
          this.uploadExcelLoading = true

          Api.importExcel({ fileId, question_id: this.queryParam.question_id }).then(({ message }) => {
            this.handleRefresh()
            this.$message.success(message)
          })
            .finally(() => {
              this.uploadExcelLoading = false
            })
        })
        .catch((err) => {
          this.uploadExcelLoading = false
        })
    },

    open ({ question_name, question_id }) {
      this.title = question_name + ' - 题目列表'
      this.queryParam.question_id = question_id
      this.visible = true
    },

    cancel () {
      this.visible = false
      this.queryParam = {}
    },

    downloadExcel () {
      window.open(window.location.origin + '/assets/download/导入题目模板.xlsx')
      // const domin = publicConfig.DOMIN
      // const a = document.createElement('a')
      // a.href = window.location.origin + '/assets/download/导入题目模板.xlsx'
      // a.download = '导入题目模板.xlsx'
      // a.click()
    },
    showAutoModel () {
      this.$refs.AutoModel.open()
    },
    /**
     * 添加
     */
    handleAdd () {
      this.$refs.SaveForm.add()
    },

    /**
     * 编辑
     */
    handleEdit (item) {
      this.$refs.SaveForm.edit(item)
    },

    handleDelete (item) {
      Api.deleted({ apiId: item['qt_id'] })
        .then((result) => {
          this.$message.success(result.message, 1.5)
          this.handleRefresh()
        })
    },

    /**
     * 搜索
     */
    handleSeachSubmit (value) {
      this.queryParam = {
        ...this.queryParam,
        ...value
      }
      this.handleRefresh(true)
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    }
  }
}
</script>
<style scoped lang="less">

</style>
